package com.lan.iot;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import static com.lan.iot.Activity_index.PREFS_NAME;

public class ListAdapterm extends ArrayAdapter<String> {
    private List<String> settings = new ArrayList<>();
Context c;
    public ListAdapterm(@NonNull Context context, int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
        this.settings=objects;
        this.c=context;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.item_,parent,false);
        if(position<3){
            if(position==2){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("f", "");
                        editor.commit();
                        Intent intent=new Intent();
                        intent.setClass(c,Login.class);
                        c.startActivity(intent);
                    }
                });
            }
            if(position==1){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent();
                        intent.setClass(c,Resetpsw.class);
                        c.startActivity(intent);
                    }
                });
            }
            if(position==0){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent();
                        intent.setClass(c,About.class);
                        c.startActivity(intent);
                    }
                });
            }
        String fruit=settings.get(position);//得到当前项的 String 实例
        //为每一个子项加载设定的布局
        //分别获取 image view 和 textview 的实例
        TextView fruitprice=view.findViewById(R.id.text1);
        // 设置要显示的图片和文字
        fruitprice.setText(fruit);
        fruitprice.setTextColor(0xFF000000);
        return view;}
        else return view;
    }
}
